package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.mapper.TrackStatMapper;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.album.TrackStat;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.annotation.Target;
import java.math.BigDecimal;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class TrackInfoServiceImpl extends ServiceImpl<TrackInfoMapper, TrackInfo> implements TrackInfoService {

	@Autowired
	private TrackInfoMapper trackInfoMapper;

	@Autowired
	private AlbumInfoMapper albumInfoMapper;

	@Autowired
	private VodService vodService;

	@Autowired
	private TrackStatMapper trackStatMapper;



	@Transactional(rollbackFor = Exception.class)
	@Override
	public void saveTrackInfo(Long userId, TrackInfoVo trackInfoVo) {
		//1.保存声音信息
		//1.1先将声音的vo对象转换为po对象
		TrackInfo trackInfo = BeanUtil.copyProperties(trackInfoVo, TrackInfo.class);
		//1.2 设置声音对象中：来源，用户id，状态
		trackInfo.setUserId(userId);
		trackInfo.setSource(SystemConstant.TRACK_SOURCE_USER);
		trackInfo.setStatus(SystemConstant.TRACK_STATUS_PASS);
		//1.2.1 根据专辑的id查询专辑关联的声音数量
		AlbumInfo albumInfo = albumInfoMapper.selectById(trackInfo.getAlbumId());
		Assert.notNull(albumInfo,"专辑不存在");
		//1.2.2 用于计算新增的声音序号
		Integer includeTrackCount = albumInfo.getIncludeTrackCount();
		trackInfo.setOrderNum(includeTrackCount+1);
		//1.3 远程调用云点播获取平台的唯一标识
		TrackMediaInfoVo trackMediaInfoVo = vodService.getMediaInfo(trackInfo.getMediaFileId());
		if (trackMediaInfoVo!=null){
			trackInfo.setMediaSize(trackMediaInfoVo.getSize());
			trackInfo.setMediaType(trackMediaInfoVo.getType());
			trackInfo.setMediaDuration(BigDecimal.valueOf(trackMediaInfoVo.getDuration()));
		}
		//获取审核装填
		trackInfo.setStatus(SystemConstant.TRACK_STATUS_PASS);
		//1.4 新增声音记录得到声音的id
		trackInfoMapper.insert(trackInfo);
		Long trackInfoId = trackInfo.getId();
		//2.更新声音所属专辑包含声音数量
		albumInfo.setIncludeTrackCount(albumInfo.getIncludeTrackCount() + 1);
		albumInfoMapper.updateById(albumInfo);
		//3.新增声音统计记录
		this.saveTrackStat(trackInfoId,SystemConstant.TRACK_STAT_PLAY,0);
		this.saveTrackStat(trackInfoId,SystemConstant.TRACK_STAT_COLLECT,0);
		this.saveTrackStat(trackInfoId,SystemConstant.TRACK_STAT_PRAISE,0);
		this.saveTrackStat(trackInfoId,SystemConstant.TRACK_STAT_COMMENT,0);

	}

	@Override
	public void saveTrackStat(Long trackInfoId, String trackStatPlay, int num) {
		TrackStat trackStat = new TrackStat();
		Long trackId;
		trackStat.setTrackId(trackInfoId);
		trackStat.setStatType(trackStatPlay);
		trackStat.setStatNum(num);
		trackStatMapper.insert(trackStat);
	}

	@Override
	public Page<TrackListVo> findUserTrackPage(TrackInfoQuery trackInfoQuery, Page<TrackListVo> pageInfo) {
		Page<TrackListVo> trackInfoList= trackInfoMapper.findUserTrackPage(trackInfoQuery,pageInfo);
		return trackInfoList;
	}


	@Override
	public void updateTrackInfo(Long id, TrackInfoVo trackInfoVo) {
		//修改声音
		//查询该id下是否声音
		TrackInfo trackInfo = trackInfoMapper.selectById(id);
		Assert.notNull(trackInfo,"声音不存在");
		//判断声音信息是否发生变化
		String mediaFileIdBefore = trackInfo.getMediaFileId();
		String mediaFileIdAfter = trackInfoVo.getMediaFileId();
		//将声音对象拷贝到po对象中
		BeanUtil.copyProperties(trackInfoVo,trackInfo);
		if (!mediaFileIdBefore.equals(mediaFileIdAfter)){
			//如果发生了变化，获取最新的声音信息
			TrackMediaInfoVo mediaInfo = vodService.getMediaInfo(mediaFileIdBefore);
			trackInfo.setMediaDuration(new BigDecimal(mediaInfo.getDuration()));
			trackInfo.setMediaType(mediaInfo.getType());
			trackInfo.setMediaSize(mediaInfo.getSize());
			//4.调用腾讯云点播平台将旧文件进行删除-避免垃圾数据占用空间浪费资金
			log.info("+++++++++++++++++++++" + Thread.currentThread().getName());
			vodService.deleteMedia(mediaFileIdBefore);
		}
	}

	@Override
	public void removeTrackInfo(Long id) {
		//1.获取要被删除声音信息 得到 当前声音序号
		TrackInfo trackInfo = trackInfoMapper.selectById(id);
		Assert.notNull(trackInfo,"声音不存在");
		//2.根据序号修改比当前声音序号大的声音序号
		Integer orderNum = trackInfo.getOrderNum();
		Long albumId = trackInfo.getAlbumId();
		trackInfoMapper.updateOrderNum(albumId,orderNum);
		//3.删除声音记录
		trackInfoMapper.deleteById(id);
		//4.专辑包含声音数量减少
		AlbumInfo albumInfo = albumInfoMapper.selectById(trackInfo.getAlbumId());
		albumInfo.setIncludeTrackCount(albumInfo.getIncludeTrackCount()-1);
		albumInfoMapper.updateById(albumInfo);
		//5.调用腾讯点播平台将音频文件删除
		vodService.deleteMedia(trackInfo.getMediaFileId());
		// 6 删除声音统计信息
		LambdaQueryWrapper<TrackStat> lambdaQueryWrapper = new LambdaQueryWrapper();
		lambdaQueryWrapper.eq(TrackStat::getTrackId,id);
		trackStatMapper.delete(lambdaQueryWrapper);
	}
}
